<template>
  <div class="app-container">
    <el-card shadow="never">
      <!-- <div slot="header" class="clearfix">
        <span style="font-weight: 700">代码仓库列表</span>
      </div> -->
      <el-button class="filter-item mb20" type="primary" @click="handleCreate">创建代码仓库</el-button>
      <el-input v-model="search" class="search" placeholder="输入关键字" />
      <el-table
        v-loading="listLoading"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
        :header-cell-style="{background:'#f9f9f9',color:'#606266'}"
        header-align="center"
        :data="tableData.filter(data => !search || data.name.toLowerCase().includes(search.toLowerCase()))"
        style="width: 100%"
      >
        <el-table-column label="ID" prop="id" header-align="center" align="center" />
        <el-table-column label="仓库名" prop="name" header-align="center" align="center" />
        <el-table-column label="仓库简介" prop="description" header-align="center" align="center" />
        <el-table-column label="默认分支" prop="default_branch" header-align="center" align="center" />
        <el-table-column
          label="最后更新"
          prop="last_activity_at"
          :formatter="handleCreateTime"
          header-align="center"
          align="center"
        />
        <el-table-column width="200" header-align="center" align="center">
          <template slot="header" slot-scope="scope">操作</template>
          <template slot-scope="{ row, $index }">
            <a href="javascript:;" class="operation" @click="handleCodeEdit($index, row)">编辑</a>
            <a href="javascript:;" class="operation" @click="handleCodeSet($index, row)">设置</a>
            <a href="javascript:;" class="operation" @click="handleDeleteRepos(row.ID, $index)">删除</a>
          </template>
        </el-table-column>
      </el-table>
      <code-repos-dialog
        :dialog-form-visible="dialogFormVisible"
        :code-data="codeData"
        :long-path="longPath"
        :visibility-options="visibilityOptions"
        @handleGitList="handleGitList"
        @submitGitList="submitGitList"
      />
    </el-card>
  </div>
</template>

<script>
import { getGitList, deleteReop } from '@/utils/apiPath';
import { postData } from '../../api/requestData';
import CodeReposDialog from '../git/components/CodeReposDialog';
import { Message } from 'element-ui';
import { parseTime } from '@/utils/index';
export default {
  components: { CodeReposDialog },
  data() {
    return {
      listLoading: true,
      tableData: [],
      longPath: '',
      search: '',
      dialogFormVisible: false,
      codeData: {
        Name: '',
        Path: '',
        Description: '',
        Visibility: 'private',
        InitializeWithReadme: false
      },
      visibilityOptions: [
        {
          value: 'private',
          label: '私有'
        },
        {
          value: 'internal',
          label: '内部'
        },
        {
          value: 'public',
          label: '公开'
        }
      ]
    };
  },
  watch: {
    'codeData.Name': function(val) {
      this.codeData.Path = val;
    }
  },
  created() {
    this.handleGitList();
  },
  methods: {
    handleGitList() {
      this.listLoading = true;
      getGitList()
        .then((res) => {
          this.listLoading = false;
          this.tableData = res.data.list;
          this.longPath = res.data.longPath;
        })
        .catch(() => {
          this.listLoading = false;
        });
    },
    handleCodeSet(index, row) {
      this.$router.push({
        path:
          '/git/code/' +
          row.id +
          '/' +
          row.name +
          '/' +
          row.default_branch +
          '/root'
      });
    },
    handleCodeEdit(index, row) {
      this.codeData.Name = row.name;
      this.codeData.Path = row.path;
      this.codeData.Description = row.description;
      this.codeData.Visibility = row.visibility;
      this.codeData.InitializeWithReadme = row.readme_url !== '';
      this.dialogFormVisible = true;
    },
    handleDeleteRepos(index, row) {
      this.$confirm('确认删除此仓库?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonType: 'warning',
        cancelButtonType: 'text',
        type: 'warning'
      }).then(() => {
        deleteReop({ id: row.id }).then((res) => {
          this.tableData.splice(index, 1);
          Message({
            message: res.message,
            type: 'success',
            duration: 1000
          });
        });
      });
    },
    resetTemp() {
      this.codeData = {
        Name: '',
        Path: '',
        Description: '',
        Visibility: 'private',
        InitializeWithReadme: false
      };
    },
    handleCreate() {
      this.resetTemp();
      this.dialogFormVisible = true;
    },
    handleCreateTime(row) {
      return parseTime(row.last_activity_at);
    },
    submitGitList() {
      this.dialogFormVisible = false;
      this.handleGitList();
    }
  }
};
</script>
<style scoped lang="scss">
@import '../../styles/main.css';
.search {
  width: 240px;
  float: right;
}
.operation {
  margin-right: 10px;
  color: #409eff;
}
</style>
